//
//  MyTakeOrderDetailVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2021/1/27.
//  Copyright © 2021 吕晶. All rights reserved.
//

#import "MyTakeOrderDetailVC.h"
#import "MyTakeOrderDetailMainModel.h"
#import "MyTakeOrderDetailGoodCell.h"
#import "MyTakeOrderDetailInfoCell.h"
#import "MyTakeOrderDetailAddressCell.h"
#import "MyTakeOrderDetailRequireCell.h"
#import "MyTakeOrderDetailOtherCell.h"
#import "MyTakeOrderDetailGoodListVC.h"

@interface MyTakeOrderDetailVC () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) MyTakeOrderDetailMainModel *mainModel;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation MyTakeOrderDetailVC

-(MyTakeOrderDetailMainModel *)mainModel {
    if (!_mainModel) {
        _mainModel = [MyTakeOrderDetailMainModel new];
    }
    return _mainModel;
}

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64 + BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
    }
    return _bottomView;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - BottomHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakself requestForData];
        }];
        _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"img_occupy"
             titleStr:@""
            detailStr:@""];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"详情";
    [self.view addSubview:self.tableView];
    [self requestForData];
}

- (void)requestForData {
    [HttpRequest PostWithApi:LogisticsSupplierTaskManagerTiDanDetail params:@{@"dp_id":@(self.dp_id)} progress:^(NSProgress * _Nonnull progress) {
            
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.mainModel = [MyTakeOrderDetailMainModel mj_objectWithKeyValues:responseObject];
//            [self configUI];
            [self.tableView reloadData];
        }
        [self.tableView.mj_header endRefreshing];
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)configUI {
    if (self.mainModel.status == 7 || self.mainModel.status == 8 || self.mainModel.status == 9) {
        //出发、到达、完成状态下显示查看运输轨迹
        [self.view addSubview:self.bottomView];
        [self.bottomView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, _bottomView.nim_width - 60, 44)];
        [nextBtn setBackgroundColor:Color_ThemeColor];
        nextBtn.layer.masksToBounds = YES;
        nextBtn.layer.cornerRadius = 6;
        [nextBtn setImage:[UIImage imageNamed:@"icon_location"] forState:0];
        [nextBtn setTitle:@"查看运输轨迹" forState:0];
        [nextBtn setTitleColor:HexColor(0xffffff) forState:0];
        nextBtn.titleLabel.font = BoldFontOfSize(16);
        [nextBtn addTarget:self action:@selector(lookCarClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bottomView addSubview:nextBtn];
        self.tableView.frame = CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - self.bottomView.nim_height);
    }else {
        [self.bottomView removeFromSuperview];
        self.tableView.frame = CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - BottomHeight);
    }
}

- (void)lookCarClick {
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 7;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        MyTakeOrderDetailGoodCell *cell = [MyTakeOrderDetailGoodCell MyTakeOrderDetailGoodCellWithTableView:tableView];
        cell.model = self.mainModel;
        __weak_self;
        cell.detailBlock = ^{
            MyTakeOrderDetailGoodListVC *vc = [MyTakeOrderDetailGoodListVC new];
            vc.model = weakself.mainModel;
            [weakself.rt_navigationController pushViewController:vc animated:YES];
        };
        return cell;
    }else if (indexPath.section == 1) {
        MyTakeOrderDetailInfoCell *cell = [MyTakeOrderDetailInfoCell MyTakeOrderDetailInfoCellWithTableView:tableView];
        cell.model = self.mainModel;
        return cell;
    }else if (indexPath.section == 2) {
        MyTakeOrderDetailAddressCell *cell = [MyTakeOrderDetailAddressCell MyTakeOrderDetailAddressCellWithTableView:tableView];
        cell.model = self.mainModel;
        return cell;
    }else if (indexPath.section == 3) {
        MyTakeOrderDetailRequireCell *cell = [MyTakeOrderDetailRequireCell MyTakeOrderDetailRequireCellWithTableView:tableView];
        cell.model = self.mainModel;
        return cell;
    }else if (indexPath.section == 4) {
        MyTakeOrderDetailOtherCell *cell = [MyTakeOrderDetailOtherCell MyTakeOrderDetailOtherCellWithTableView:tableView];
        cell.titleText = @"运输注意事项";
        cell.contentText = self.mainModel.remarks;
        return cell;
    }else if (indexPath.section == 5) {
        MyTakeOrderDetailOtherCell *cell = [MyTakeOrderDetailOtherCell MyTakeOrderDetailOtherCellWithTableView:tableView];
        cell.titleText = @"随附文件";
        cell.contentText = self.mainModel.fileInfo;
        return cell;
    }else if (indexPath.section == 6) {
        MyTakeOrderDetailOtherCell *cell = [MyTakeOrderDetailOtherCell MyTakeOrderDetailOtherCellWithTableView:tableView];
        cell.titleText = @"备注";
        cell.contentText = self.mainModel.note;
        return cell;
    }
    UITableViewCell *cell = [UITableViewCell new];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        MyTakeOrderDetailGoodCell *cell = [MyTakeOrderDetailGoodCell MyTakeOrderDetailGoodCellWithTableView:tableView];
        cell.model = self.mainModel;
        return cell.height;
    }else if (indexPath.section == 1) {
        MyTakeOrderDetailInfoCell *cell = [MyTakeOrderDetailInfoCell MyTakeOrderDetailInfoCellWithTableView:tableView];
        cell.model = self.mainModel;
        return cell.height;
    }else if (indexPath.section == 2) {
        MyTakeOrderDetailAddressCell *cell = [MyTakeOrderDetailAddressCell MyTakeOrderDetailAddressCellWithTableView:tableView];
        cell.model = self.mainModel;
        return cell.height;
    }else if (indexPath.section == 3) {
        MyTakeOrderDetailRequireCell *cell = [MyTakeOrderDetailRequireCell MyTakeOrderDetailRequireCellWithTableView:tableView];
        cell.model = self.mainModel;
        return cell.height;
    }else if (indexPath.section == 4) {
        MyTakeOrderDetailOtherCell *cell = [MyTakeOrderDetailOtherCell MyTakeOrderDetailOtherCellWithTableView:tableView];
        cell.contentText = self.mainModel.remarks;
        return cell.height;
    }else if (indexPath.section == 5) {
        MyTakeOrderDetailOtherCell *cell = [MyTakeOrderDetailOtherCell MyTakeOrderDetailOtherCellWithTableView:tableView];
        cell.contentText = self.mainModel.fileInfo;
        return cell.height;
    }else if (indexPath.section == 6) {
        MyTakeOrderDetailOtherCell *cell = [MyTakeOrderDetailOtherCell MyTakeOrderDetailOtherCellWithTableView:tableView];
        cell.contentText = self.mainModel.note;
        return cell.height;
    }
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
        view.backgroundColor = Color_backgroundColor;
        return view;
    }
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 10;
    }
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

@end
